
/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

package com.jy.rhin.facade.ws.document.management;

import cn.org.chiss.rhin._2015.base.UUID;
import cn.org.chiss.rhin._2015.base.document.Association;
import cn.org.chiss.rhin._2015.base.document.DocumentEntry;
import cn.org.chiss.rhin._2015.base.document.Folder;
import cn.org.chiss.rhin._2015.base.document.SubmissionSet;
import cn.org.chiss.rhin._2015.base.object.RegObject;
import cn.org.chiss.rhin._2015.document.management.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jy.framework.base.utils.Serializer;
import com.jy.rhin.application.assembler.DocumentStorageAssembler;
import com.jy.rhin.application.document.DocumentQueryApplication;
import com.jy.rhin.application.infra.DocumentApplicationService;
import com.jy.rhin.config.NamespaceConstants;
import com.jy.rhin.facade.ws.basic.wsEntry.exception.RhinWSException;
import com.jy.rhin.infra.exception.RhinAggrErrorType;
import com.jy.rhin.sdk.dto.document.DocumentSubmissionSetDto;
import com.jy.rhin.support.aspect.AuditLog;
import com.jy.rhin.support.util.Asserts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;

/**
 * This class was generated by Apache CXF 3.5.1
 * 2022-04-20T09:29:06.792+08:00
 * Generated source version: 3.5.1
 *
 * @author dhl
 */

@Slf4j
@WebService(
        serviceName = "DocumentManagementServer",
        portName = "DocumentManagementPort",
        targetNamespace = NamespaceConstants.NS_RHIN,
        endpointInterface = "com.jy.rhin.facade.ws.document.management.DocumentManagement")
@Component
public class DocumentManagementPortImpl implements DocumentManagement {
    @Resource
    private DocumentApplicationService documentApplicationService;
    @Resource
    DocumentQueryApplication documentQueryApplication;

    /**
     * 索引文档
     *
     * @param message
     * @return
     */
    @Override
    @AuditLog
    public DocumentStoredQueryResponse documentStoredQuery(DocumentStoredQuery message) {
        log.info("Executing operation documentStoredQuery:{}", Serializer.serialize(message));
        return documentQueryApplication.getDocumentStoredQueryResponse(message);
    }

    /**
     * @param message
     * @return
     */
    @Override
    @AuditLog
    public UpdateDocumentSetResponse updateDocumentSet(UpdateDocumentSet message) {
        log.info("Executing operation updateDocumentSet:{}", Serializer.serialize(message));
        Asserts.nonNull(message, RhinAggrErrorType.UPDATE_DOCUMENTSET_NOT_FOUND_FAULT);
        Asserts.nonNull(message.getSubmissionSet(), RhinAggrErrorType.UPDATE_DOCUMENTSET_NOT_FOUND_FAULT);
        Asserts.nonNull(message.getSubmissionSet().getId(), RhinAggrErrorType.UPDATE_DOCUMENTSET_INFORMATION_INCORRECT_FAULT);
        Asserts.nonNull(message.getSubmissionSet().getId().getValue(), RhinAggrErrorType.UPDATE_DOCUMENTSET_INFORMATION_INCORRECT_FAULT);
        Asserts.nonNull(message.getSubmissionSet().getUniqueId(), RhinAggrErrorType.UPDATE_DOCUMENTSET_INFORMATION_INCORRECT_FAULT);
        Asserts.nonNull(message.getSubmissionSet().getUniqueId().getValue(), RhinAggrErrorType.UPDATE_DOCUMENTSET_INFORMATION_INCORRECT_FAULT);
        DocumentSubmissionSetDto submissionSetDto = DocumentStorageAssembler.buildSubmissionSet(message.getSubmissionSet(), true);
        Long resp = documentApplicationService.documentUpdate(submissionSetDto);
        UpdateDocumentSetResponse result = new UpdateDocumentSetResponse();
        UUID uuuid = new UUID();
        uuuid.setValue(resp.toString());
        result.setSubmissionSetUUID(uuuid);
        return result;
    }

    /**
     * 文档注册
     *
     * @param message
     * @return
     */
    @Override
    @AuditLog
    public RegisterDocumentSetResponse registerDocumentSet(RegisterDocumentSet message) {
        RegisterDocumentSetResponse result = new RegisterDocumentSetResponse();
        try {
            log.info("Executing operation registerDocumentSet:{}", Serializer.serialize(message));
            Asserts.nonNull(message, RhinAggrErrorType.REGISTER_DOCUMENTSET_NOT_FOUND_FAULT);
            Asserts.nonNull(message.getSubmissionSet(), RhinAggrErrorType.REGISTER_DOCUMENTSET_NOT_FOUND_FAULT);
            DocumentSubmissionSetDto submissionSetDto = DocumentStorageAssembler.buildSubmissionSet(message.getSubmissionSet(), true);
            submissionSetDto.setJsonText(getJsonText(message.getSubmissionSet()));
            Long resp = documentApplicationService.documentRegister(submissionSetDto);
            UUID uuuid = new UUID();
            uuuid.setValue(resp.toString());
            result.setSubmissionSetUUID(uuuid);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new RhinWSException("RegisterDocumentSetFault", ex.getMessage());
        }
        return result;
    }

    /**
     * 因消息通知以及查询 需要通过解析表字段json_text内容来匹配，所以在这里把附件分开
     *
     * @param submissionSet
     * @return
     */
    private String getJsonText(SubmissionSet submissionSet) {
        JSONObject submissionSetJson = JSON.parseObject(JSON.toJSONString(submissionSet));
        List<Folder> folderList = new ArrayList<>();
        List<DocumentEntry> documentEntryList = new ArrayList<>();
        List<Association> associationList = new ArrayList<>();
        List<RegObject> jsonArray = submissionSet.getFolderOrDocumentEntryOrAssociation();
        for (RegObject regObject : jsonArray) {
            if (regObject instanceof Folder) {
                folderList.add((Folder) regObject);
            } else if (regObject instanceof DocumentEntry) {
                documentEntryList.add((DocumentEntry) regObject);
            } else if (regObject instanceof Association) {
                associationList.add((Association) regObject);
            }
        }
        submissionSetJson.remove("folderOrDocumentEntryOrAssociation");
        submissionSetJson.put("folderList", folderList);
        submissionSetJson.put("documentEntryList", documentEntryList);
        submissionSetJson.put("associationList", associationList);
        return submissionSetJson.toJSONString();
    }

}
